import Vue from 'vue'
import Vuex from 'vuex'
// import { getRequest } from '../utils/api'
// import SockJs from 'sockjs-client'
// import Stomp from 'stompjs'
// import { Notification } from 'element-ui'

Vue.use(Vuex)

const now = new Date()

const store = new Vuex.Store({
    //  全局对象
    state: {
        routes: [],
        sessions: {},
        admins: [],
        currentSession: null,
        currentAdmin: JSON.parse(window.sessionStorage.getItem('user')),
        filterKey: '',
        stomp: null,
        isDot: {}
    },
    //  改变state里对应值的方法
    mutations: {
        initRoutes(state, data) {
            state.routes = data;
        },
        changecurrentSession(state, currentSession) {
            state.currentSession = currentSession;
            Vue.set(state.isDot, state.currentAdmin.username + '#' + state.currentSession.username, false);
        },
        addMessage(state, msg) {
            let mss = state.sessions[state.currentAdmin.username + "#" + msg.to];
            if (!mss) {
                Vue.set(state.sessions, state.currentAdmin.username + '#' + msg.to, []);
            }
            state.sessions[state.currentAdmin.username + "#" + msg.to].push({
                content: msg.content,
                date: new Date(),
                self: !msg.notSelf
            })
        },


        INIT_ADMIN(state, admin) {
            state.currentAdmin = admin
        }
    },

    modules: {}
})

store.watch(function(state) {
    return state.sessions
}, function(val) {
    console.log('CHANGE:', val);
    localStorage.setItem('vue-chat-session', JSON.stringify(val));
}, {
    deep: true /*这个貌似是开启watch监测的判断,官方说明也比较模糊*/
})


export default store;